package com.syq.controller.admin;


import com.syq.entity.Order;
import com.syq.entity.User;
import com.syq.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package ${PACKAGE_NAME}
 * @description: ${TODO}
 * @date 2022/6/28 10:03
 */
@RestController
@WebServlet(name = "GetAllOrderServlet",urlPatterns = "/getAllOrder")
public class GetAllOrderServlet extends HttpServlet {

    private static OrderService orderService;

    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1判断是否登录 admin
        User admin= (User) request.getSession().getAttribute("admin");
        if(admin==null){
            response.sendRedirect(request.getContextPath()+"/admin/login.jsp");
            return;
        }

        //2调用方法
        try {
            List<Order> orderList= orderService.findAll();
            //3放入域中
            request.setAttribute("orderList",orderList );
            //4转发
            request.getRequestDispatcher("/admin/showAllOrder.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect(request.getContextPath()+"/admin/admin.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
